<div class="row-content am-cf">
    <div class="row">

        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title am-cf">用户设置</div>
                </div>
                <div class="widget-body am-fr">
                    <form class="am-form tpl-form-line-form" method="post"
                          action="<?= url('/store/user/setting') ?>/userId/<?= $user['user_id'] ?>">
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label">省 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <input type="text" class="tpl-form-input" name="user[province]"
                                       value="<?= $user['province'] ?>">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label">市 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <input type="text" class="tpl-form-input" name="user[city]"
                                       value="<?= $user['city'] ?>">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label">县 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <input type="text" class="tpl-form-input" name="user[zone]"
                                       value="<?= $user['zone'] ?>">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label">代理手机 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <input type="text" class="tpl-form-input" name="user[mobile]"
                                       value="<?= $user['mobile'] ?>">
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label">真实姓名 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <input type="text" class="tpl-form-input" name="user[real_name]"
                                       value="<?= $user['real_name'] ?>">
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">代理级别 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <select name="user[agent_level]" required id="level"
                                        data-am-selected="{searchBox: 1, btnSize: 'sm',  placeholder:'请选择代理级别'}">
                                    <?php if (isset($agents)): foreach ($agents as $level => $agent): ?>
                                        <option value="<?= $level ?>"
                                            <?php if ($user['agent_level'] == $level): ?>
                                                selected="selected"
                                            <?php endif; ?>
                                        >
                                            <?= $agent['name'] ?>
                                        </option>
                                    <?php endforeach; endif; ?>
                                </select>
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label">上级代理 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <p><?php if (isset($upper)) { ?>
                                        <span id="upperName"><?php echo $upper['real_name']; ?><?php echo $upper['mobile']; ?></span>
                                    <?php } else { ?>
                                        <span id="upperName">无</span>
                                    <?php } ?>
                                    <small class="am-margin-left-xs">
                                        <a href="javascript:void(0)" onclick="searchAgent()">设置上级</a>
                                    </small> |
                                    <small class="am-margin-left-xs">
                                        <a href="javascript:void(0)" onclick="clearAgent()">清空</a>
                                    </small>
                                </p>
                                <input type="hidden" name="user[upper_id]" id="upper-id"
                                       value="<?= $user['upper_id'] ?>"/>
                            </div>
                        </div>


                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-u-lg-2 am-form-label"> 二维码 </label>
                            <div class="am-u-sm-9 am-u-end">
                                <img id="share_img" src="<?= $user['share_img'] ?>" style="max-width: 300px">
                                <input id="share_img_input" type="hidden" name="user[share_img]"
                                       value="<?= $user['share_img'] ?>">
                                <p style="font-size: small;color: gray">
                                    <!--                                        <span class="am-icon-info"></span>-->
                                    <span class="btn am-btn am-btn-success am-btn-sm"
                                          onclick="refreshShareImg()">刷新分享图片</span>
                                </p>
                            </div>
                        </div>


                        <div class="am-form-group">
                            <div class="am-u-sm-9 am-u-sm-push-3 am-margin-top-lg">
                                <button type="submit" class="j-submit am-btn am-btn-secondary">提交
                                </button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    function searchAgent() {
        layer.open({
            type: 2,
            area: ['60%', '60%'],
            content: '<?=url("/store/user/agents/userId/")?>' + '<?=$user['user_id']?>' + '/level/' + $('#level').val()
        });
    }

    function refreshShareImg() {
        layer.msg('刷新中', {
            icon: 16
            , shade: 0.01
            , time: '-1'
        });
        $.ajax({
            url: '<?=url("/store/user/updateShareImg")?>',
            data: {
                userId:<?=$user['user_id']?>,
                level: $('#level').val()
            },
            dataType: 'json',
            type: 'post',
            success: function (res) {
                if (res.code === 1) {
                    $('#share_img').attr('src', res.data.share_img);
                    $('#share_img_input').val(res.data.share_img);
                    layer.msg(res.msg);
                } else {
                    layer.alert(res.msg);
                }
            }
        })
    }


    function clearAgent() {
        if (confirm("确定要清空当前用户的上级吗？")) {
            $.ajax({
                url: '<?=url("/store/user/clear_agent/userId/")?>' + '<?=$user['user_id']?>',
                dataType: 'json',
                type: 'post',
                data: {},
                success: function (res) {
                    if (res.code === 1) {
                        layer.alert("清空成功");
                        $('#upper-id').val(0);
                        $('#upperName').html("无");
                    } else {
                        layer.alert(res.msg);
                    }
                },
                error: function (e) {
                    console.log(e)
                }
            })
        }
    }
</script>